<?php

/**
 * Pagina form.
 *
 * @package    form
 * @subpackage Pagina
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class PaginaForm extends BasePaginaForm
{
  	public function configure()
  	{
  		parent::configure();
		
  		$options = array('' => '-- Selecione um tipo --','1'=>'Institucional', '2'=>'Serviços', '3'=>'Para o público');
		$this->widgetSchema['tp_pagina'] = new sfWidgetFormChoice(
			array('choices'=>$options, 'expanded'=>false, 'multiple'=>false)
		);
		
  		$options = array('N'=>'Não', 'S'=>'Sim');
		$this->widgetSchema['st_pagina'] = new sfWidgetFormChoice(
			array('choices'=>$options, 'expanded'=>true, 'multiple'=>false)
		);
		
		$this->widgetSchema['ds_titulo'] = new sfWidgetFormInput(
			array(),
			array('class' => 'required', 'size' => '100', 'maxlength' => '50')
		);
		
  		$this->widgetSchema['ds_conteudo'] = new sfWidgetFormTextareaTinyMCE(
			array(
				'width'  => 700,
                'height' => 400,
                'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
            )
        );
        
        /*
       	 * Formulário embutido de mídia
       	 */
      	$form = new MidiaPaginaForm(null, array(
			'item' => $this->getObject(),//nome do objeto
			'size'    => 10//quantidade de registros
		));
        $this->embedForm('midia', $form);
  		
  		$this->widgetSchema->setLabels(array(
  		      	'id_pagina'   => 'ID#',
		      	'tp_pagina'   => 'Tipo',
		      	'ds_titulo'   => 'Titulo',
		      	'ds_conteudo' => 'Conteudo',
		      	'st_pagina'   => 'Publicar',
  				'slugpagina'  => 'Slug',
		      	'created_at'  => 'Criado em',
		      	'updated_at'  => 'Atualizado em',
		));
  	}
  	
	public function saveEmbeddedForms($con = null, $forms = null)
	{
		if (null === $forms)
		{
			$midia = $this->getValue('midia');
			$forms = $this->embeddedForms;
			foreach ($this->embeddedForms['midia'] as $name => $form)
			{
				if (!isset($midia[$name]))
					unset($forms['midia'][$name]);
			}
		}
		parent::saveEmbeddedForms($con, $forms);
		 
		//Criando as Thumbs
		foreach($this->getObject()->getMidia() as $midia)
		{
			$thumb   = new Midia();
			$thumb->createThumb($midia);
		}

		return;
	}
}